<?php
/**
 * Created by PhpStorm.
 * User: william
 * Date: 16-8-30
 * Time: 下午2:15
 */
namespace V1;
class C_CollectionList extends \ResourceClass {
    public $isActive = true;
    public $remark = '用户收藏商品列表';

    public function get (array $req, array $post, &$result) {
        if (!isset($req['uid'])) {
            return 'require user uid';
        }
        if (!isset($req['userType'])) {
            return 'require user type';
        }
        if (!isset($req['k']) || !isset($req['t'])) {
            return 'data error';
        }

        if (!isset($req['enterpriseId'])) {
            return 'require enterprise id';
        }
        if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'], $req['enterpriseId'], $req['t'], $req['k']) == false) {
            return 'data illegal';
        }
        if (!isset($req['channelId'])) {
            return 'require channel id';
        }
        $l = isset($req['l']) ? $req['l'] : 100000;
        $o = isset($req['o']) ? $req['o'] : 0;
        $collection_product_id = 0;
        //$collectionlist = CollectionModel::getCustomerCollection($req['uid'],$req['channelId']);
        //获取收藏列表
        $customer_collection_handler = new \Biz\CustomerCollectionHandler();
        $talabtion_collection = new \Cls\Tabulation();
        $talabtion_collection -> setWhere('customer_id',$req['uid']);
        $talabtion_collection -> setWhere('channel_id',$req['channelId']);
        $talabtion_collection -> setWhere('is_del',1);
        $talabtion_collection->setLimit(9999);
        $customer_collection_handler ->getCustomerCollectionList($talabtion_collection);
        $collectionlist = $talabtion_collection->getData();
        $product_list = array();
        foreach ($collectionlist as $key => $collection_cls){
            $product_handler = new \Biz\ProductHandler();
            $product_list[] = $product_handler -> getProductById($collection_cls->getProductId());
        }
        // 获取当前渠道信息
        $channel_cls = \Biz\ChannelHandler::getChannelById($req['channelId']);
        $discount = $channel_cls->getDiscount();
        if(!empty($product_list)){
            /**
             * @var \Cls\Product\Product $product_cls
             */
            foreach ($product_list as $product_cls) {
                $product_cls_temp = new \stdClass();
                $product_cls_temp->id = $product_cls->getId();
                $product_cls_temp->enterprise_id = $product_cls->getEnterpriseId();
                $product_cls_temp->brand_id = $product_cls->getBrandId();
                $product_cls_temp->name = $product_cls->getName();
                $product_cls_temp->icon_url = $product_cls->dealIconUrl();
                $product_cls_temp->repertory_warning = $product_cls->getStockWarning();
                $product_cls_temp->individual_packing = $product_cls->getSellUnitString();// 老版本
                $product_cls_temp->mass_packing = $product_cls->getSellUnitString();  // 老版本
                $product_cls_temp->sell_unit_string = $product_cls->getSellUnitString();
                $product_cls_temp->remark = $product_cls->getDescription();
                $product_cls_temp->insert_time = $product_cls->getCreateTime();
                $product_cls_temp->is_del = $product_cls->getDelStatus();
                $brand_cls = $product_cls->getBrandInfo();
                if ($brand_cls instanceof \Cls\Brand\Brand) {
                    $brand_name = $brand_cls->getName();
                } else {
                    $brand_name = '未知分类名称';
                }
                $product_cls_temp->brandname = $brand_name;
                $product_cls_temp->channel_price = 88888;
                $product_cls_temp->collect_stock = $product_cls->getRepertoryTotal();
                $product_cls_temp->stock_warning = $product_cls->judgeStockWaring();
                $sku_list = $product_cls->getSkuList();
                $product_cls_temp->sku_info = array();
                if (!empty($sku_list)) {
                    /**
                     * @var $sku_cls \Cls\Product\Sku
                     */
                    foreach ($sku_list as $sku_cls) {
                        $sku_temp = new \stdClass();
                        $sku_temp->id = $sku_cls->getId();
                        $sku_temp->product_id = $sku_cls->getProductId();
                        $sku_temp->imgurl = $sku_cls->dealImageUrl();
                        $sku_temp->attr = $sku_cls->dealAttr();
                        $sku_temp->code = $sku_cls->getCode();
                        $sku_temp->sellprice = $sku_cls->getChannelSellingPrice($discount);
                        $sku_temp->cost = $sku_cls->getCost();
                        $sku_temp->stock = $sku_cls->getStock();
                        $sku_temp->insert_time = $sku_cls->getCreateTime();
                        $sku_temp->update_time = $sku_cls->getUpdateTime();
                        $product_cls_temp->sku_info [] = $sku_temp;
                    }
                }
                $result['product_list'][] = $product_cls_temp;
            }
        }

    }
}